﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ComputerVision.Data;
using ComputerVision.Data.Repositories;

namespace ComputerVision.Services
{
    public class PhuongTienCoSoService : IService<PhuongTienCoSo>
    {
        private readonly IEntityRepository<PhuongTienCoSo> _phuongTienCoSoRepository;
        public PhuongTienCoSoService(IEntityRepository<PhuongTienCoSo> phuongTienCoSoRepository)
        {
            _phuongTienCoSoRepository = phuongTienCoSoRepository;
        }

        public PhuongTienCoSo Create(PhuongTienCoSo t)
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("entity");
                _phuongTienCoSoRepository.InsertOnCommit(t);
                _phuongTienCoSoRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine;

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return t;
        }

        public PhuongTienCoSo Update(PhuongTienCoSo t)
        {
            PhuongTienCoSo item;
            try
            {
                item = _phuongTienCoSoRepository.Get(t.Id);
                item.Ten = t.Ten;
                item.SoLuong = t.SoLuong;
                item.TinhTrangHoatDong_Tot = t.TinhTrangHoatDong_Tot;
                item.TinhTrangHoatDong_TrungBinh = t.TinhTrangHoatDong_TrungBinh;
                item.TinhTrangHoatDong_Kem = t.TinhTrangHoatDong_Kem;
                item.TinhTrangHoatDong_HuHong = t.TinhTrangHoatDong_HuHong;
                item.Vitri = t.Vitri;
                item.PhanLoaiChiTietPhuongTienId = t.PhanLoaiChiTietPhuongTienId;
                item.PhieuDieuTraCoSoId = t.PhieuDieuTraCoSoId;
                //item.Created = DateTime.Now;
                //item.CreatedBy = t.CreatedBy;
                _phuongTienCoSoRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine +
                               string.Format("Property: {0} Error: {1}", validationError.PropertyName,
                                   validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return item;
        }

        public PhuongTienCoSo Detail(Guid id)
        {
            var item = _phuongTienCoSoRepository.Get(id);
            return item;
        }

        public List<PhuongTienCoSo> ToList()
        {
            var item = _phuongTienCoSoRepository.GetAll().ToList();
            return item;
        }

        public void Delete(PhuongTienCoSo t)
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("entity");
                _phuongTienCoSoRepository.DeleteOnCommit(t);
                _phuongTienCoSoRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;

                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine + string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
        }
    }
}
